package com.geostar.gmp.mina;
import java.net.InetSocketAddress;  
import java.nio.charset.Charset;  
  
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;  
import org.apache.mina.core.future.ConnectFuture;  
import org.apache.mina.filter.codec.ProtocolCodecFilter;  
import org.apache.mina.transport.socket.nio.NioSocketConnector;  
  
public class MinaClient {  
    private static final int bindPort=8899;  
    
    // 创建一个socket连接      
    private   NioSocketConnector connector;
    
    public void toLinkServer(String ip){
    	initLink();
    	loadHandler();
    	
    	link(ip);
    }
    
    /**
     * 目的: 初始化连接器
     */
    private void initLink(){
    	connector = new NioSocketConnector();  
        // 获取过滤器链          
        DefaultIoFilterChainBuilder chain=connector.getFilterChain();  
          
        ProtocolCodecFilter filter= new ProtocolCodecFilter(new HCoderFactory(Charset.forName("UTF-8")));   
        // 添加编码过滤器 处理乱码、编码问题    
        chain.addLast("objectFilter",filter);  
    }
    
    /**
     * 目的: 加载handler
     *
     */
    private void loadHandler() {
    	   // 消息核心处理器       
        connector.setHandler(new MinaClientHanlder());  
    }
    
    /**
     * 目的: 连接
     * @param ip 需要连接的电脑IP
     *
     */
    private void link(String ip) {
    	 // 设置链接超时时间       
        connector.setConnectTimeoutCheckInterval(30);  
        // 连接服务器，知道端口、地址      
        ConnectFuture cf = connector.connect(new InetSocketAddress(ip,bindPort));  
        // 等待连接创建完成      
        cf.awaitUninterruptibly();  
        cf.getSession().getCloseFuture().awaitUninterruptibly();  
        connector.dispose();  
    }
    
    public static void main(String[] args) {
		new MinaClient().toLinkServer("127.0.0.1");
	}
}  